$("#sub").click(function () {
    const username=$("#username");
    const password=$("#password");
    const verifyCode=$("#verifyCode");
    const ALError=$("#ALError");
    if (username.val() === "") {
        username.addClass("input change");
        username.attr("placeholder", "手机号不能为空!");
        return;
    }
    if (password.val() === "") {
        password.addClass("input change");
        password.attr("placeholder", "密码不能为空");
        return;
    }
    if (verifyCode.val() === "") {
        verifyCode.addClass("input change");
        verifyCode.attr("placeholder", "验证码不能为空");
        return;
    }

    // 校验手机号格式
    const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if (!reg.test(username.val())) {
        username.val(null);
        username.addClass("input change");
        username.attr("placeholder", "手机号格式错误!");
        return;
    }
    // 获取“记住我”复选框的状态
    const rememberMeCheckbox = document.querySelector('input[name="rememberMe"]');
    const rememberMe = rememberMeCheckbox.checked ? 1 : 0;
    // 发送Ajax请求
    $.ajax({
        url: "/admins/login",
        type: "post",
        data: {
            tel: username.val(),
            pwd: password.val(),
            rememberMe:rememberMe,
            verifyCode: verifyCode.val()
        },
        success: function (Result) {
            $("#verifyCode").val(null);
            const time = new Date().getTime(); // 获取当前时间戳
            const verifyCodeImg = document.querySelector('#verifyCodeImg');
            verifyCodeImg.src = '/verifyCode?t=' + time; // 设置验证码图片地址
            if(Result.code !== 30001){
                ALError.css("visibility", "visible");
                ALError.text(Result.msg);
            }else if(Result.code===30001){
                $("#app").css("display","block");
                setTimeout(function() {
                    $("#app").css("display","none");
                    window.location.href="/admins/AdminMain";
                }, 1000);
            }
        },
        error: function () {
            alert("请求失败");
        }
    });
});

function refreshVerifyCode() {
    const time = new Date().getTime(); //获取当前时间戳
    const verifyCodeImg = document.querySelector('#verifyCodeImg');
    verifyCodeImg.src = '/verifyCode?t=' + time; //设置验证码图片地址
}

window.onload = function () {
    const time = new Date().getTime(); // 获取当前时间戳
    const verifyCodeImg = document.querySelector('#verifyCodeImg');
    verifyCodeImg.src = '/verifyCode?t=' + time; // 设置验证码图片地址
    if($("#msg").val()!==""){
        alert($("#msg").val());
        $("#msg").val("");
    }
}